//////////////////////////////////////////////////////////////////////////////////
//	Copyright 2011 by Yosef Grabivker, ryuho@homenetworksoft.com
//
//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.
//////////////////////////////////////////////////////////////////////////////////
#include <wx/wx.h>
#include <wx/config.h>
#include "configdata.h"

CConfigData::CConfigData()
{
	m_config = new wxConfig(RCMD_CFG_APP_ID);
}

CConfigData::~CConfigData()
{
	delete m_config;
}

//User
wxString CConfigData::GetUser()
{
	wxString strRet;
	
	if ( !m_config ) THROW_EXC(cERWrongObjectState);
	if (!m_config->Read(CFG_USER_VAL, &strRet)) THROW_EXC(cERObjectNotFound);
	
	return strRet;
}

void CConfigData::SetUser(const wxString& strVal)
{
	if ( !m_config ) THROW_EXC(cERWrongObjectState);
	m_config->Write(CFG_USER_VAL, strVal);
}

//Password
wxString CConfigData::GetPassword()
{
	wxString strRet;
	
	if ( !m_config ) THROW_EXC(cERWrongObjectState);
	if (!m_config->Read(CFG_PASSW_VAL, &strRet)) THROW_EXC(cERObjectNotFound);
		
	return strRet;	
}

void CConfigData::SetPassword(const wxString& strVal)
{
	if ( !m_config ) THROW_EXC(cERWrongObjectState);
	m_config->Write(CFG_PASSW_VAL, strVal);
}

//Protocol
cPROTOCOL CConfigData::GetProtocol()
{
	long lRet( cPROTOCOLTelnet );
	
	if ( m_config )
	{
		if ( !m_config->Read(CFG_PROTO_VAL, &lRet) )
		{
			lRet = cPROTOCOLTelnet;
		}
	}
	
	return (cPROTOCOL)lRet;	
}

void CConfigData::SetProtocol(cPROTOCOL cVal)
{
	if ( !m_config ) THROW_EXC(cERWrongObjectState);
	m_config->Write(CFG_PROTO_VAL, (long)cVal);
}


//IP
wxString CConfigData::GetIP()
{
	wxString strRet;
	
	if ( !m_config ) THROW_EXC(cERWrongObjectState);
	if (!m_config->Read(CFG_IP_VAL, &strRet)) THROW_EXC(cERObjectNotFound);
		
	return strRet;	
}

void CConfigData::SetIP(const wxString& strVal)
{
	if ( !m_config ) THROW_EXC(cERWrongObjectState);
	m_config->Write(CFG_IP_VAL, strVal);
}

////////////////////////////////////////////////////////////
bool CConfigData::GetUseSSHKeys()
{
	long lRet(0);
	
	if ( !m_config ) THROW_EXC(cERWrongObjectState);
	if ( !m_config->Read(CFG_USE_SSH_KEYS, &lRet) ) lRet = 0;
	
	return (bool)lRet;	
}

void CConfigData::SetUseSSHKeys(bool bVal)
{
	if ( !m_config ) THROW_EXC(cERWrongObjectState);
	m_config->Write(CFG_USE_SSH_KEYS, (long)bVal);
}

wxString CConfigData::GetPubKey()
{
	wxString strRet;
	
	if ( !m_config ) THROW_EXC(cERWrongObjectState);
	m_config->Read(CFG_SSH_PUB_KEY, &strRet);
		
	return strRet;	
}

void CConfigData::SetPubKey(const wxString& strVal)
{
	if ( !m_config ) THROW_EXC(cERWrongObjectState);
	m_config->Write(CFG_SSH_PUB_KEY, strVal);
}

wxString CConfigData::GetPrivKey()
{
	wxString strRet;
	
	if ( !m_config ) THROW_EXC(cERWrongObjectState);
	m_config->Read(CFG_SSH_PRV_KEY, &strRet);
		
	return strRet;	
}

void CConfigData::SetPrivKey(const wxString& strVal)
{
	if ( !m_config ) THROW_EXC(cERWrongObjectState);
	m_config->Write(CFG_SSH_PRV_KEY, strVal);
}

wxString CConfigData::GetPassPhrase()
{
	wxString strRet;
	
	if ( !m_config ) THROW_EXC(cERWrongObjectState);
	m_config->Read(CFG_SSH_PHRASE_PHR, &strRet);
		
	return strRet;	
}

void CConfigData::SetPassPhrase(const wxString& strVal)
{
	if ( !m_config ) THROW_EXC(cERWrongObjectState);
	m_config->Write(CFG_SSH_PHRASE_PHR, strVal);
}

////////////////////////////////////////////////////////////
wxString CConfigData::GetLoginPromptString()
{
	wxString strRet(wxT("login"));
	
	if ( !m_config ) THROW_EXC(cERWrongObjectState);
	m_config->Read(CFG_WELCOME_STR, &strRet);
		
	return strRet;	
}

void CConfigData::SetLoginPromptString(const wxString& strVal)
{
	if ( !m_config ) THROW_EXC(cERWrongObjectState);
	m_config->Write(CFG_WELCOME_STR, strVal);
}


wxString CConfigData::GetShellString()
{
	wxString strRet(wxT("sh"));
	
	if ( !m_config ) THROW_EXC(cERWrongObjectState);
	m_config->Read(CFG_SHELL_STR, &strRet);
		
	return strRet;	
}

void CConfigData::SetShellString(const wxString& strVal)
{
	if ( !m_config ) THROW_EXC(cERWrongObjectState);
	m_config->Write(CFG_SHELL_STR, strVal);
}

void CConfigData::SetHTTPString(const wxString& strVal)
{
	if ( !m_config ) THROW_EXC(cERWrongObjectState);
	m_config->Write(CFG_HTTP_STR, strVal);
}

wxString CConfigData::GetHTTPString()
{
	CONPARAMS def;
	wxString strRet(def.strHTTP);

	if ( !m_config ) THROW_EXC(cERWrongObjectState);
	m_config->Read(CFG_HTTP_STR, &strRet);

	return strRet;	
}

void CConfigData::SetHTTPEnable(bool bHTTPEnable)
{
	if ( !m_config ) THROW_EXC(cERWrongObjectState);
	m_config->Write(CFG_HTTP_ENABLE_VAL, bHTTPEnable);
}

bool CConfigData::GetHTTPEnable()
{
	long lRet(0);

	if ( !m_config ) THROW_EXC(cERWrongObjectState);
	if ( !m_config->Read(CFG_HTTP_ENABLE_VAL, &lRet) ) lRet = 0;

	return (bool)lRet;
}

////////////////////////////////////////////////////////////
//Port
unsigned short CConfigData::GetPort()
{
	long lRet(0);
	
	if ( !m_config ) THROW_EXC(cERWrongObjectState);
	if ( !m_config->Read(CFG_PORT_VAL, &lRet) ) THROW_EXC(cERObjectNotFound);
	
	return (unsigned short)lRet;	
}

void CConfigData::SetPort(unsigned short wVal)
{
	if ( !m_config ) THROW_EXC(cERWrongObjectState);
	
	m_config->Write(CFG_PORT_VAL, (long)wVal);
}

//Locale
long CConfigData::GetLangId()
{
	long lRet(0);
		
	if ( !m_config ) THROW_EXC(cERWrongObjectState);
	if ( !m_config->Read(CFG_LANG_VAL, &lRet) ) THROW_EXC(cERObjectNotFound);
		
	return lRet;	
}

void CConfigData::SetLangId( long val)
{
	if ( !m_config ) THROW_EXC(cERWrongObjectState);
		
	m_config->Write(CFG_LANG_VAL, val);	
}


wxString CConfigData::GetProperties()
{
	wxString strRet;
	
	if ( !m_config ) THROW_EXC(cERWrongObjectState);
	if (!m_config->Read(CFG_PROP_VAL, &strRet)) THROW_EXC(cERObjectNotFound);
		
	return strRet;	
}

void CConfigData::SetProperties( const wxString& val)
{
	if ( !m_config ) THROW_EXC(cERWrongObjectState);
	m_config->Write(CFG_PROP_VAL, val);
}

wxString CConfigData::GetPingXML()
{
	wxString strRet;
	
	if ( !m_config ) THROW_EXC(cERWrongObjectState);
	if (!m_config->Read(CFG_PING_VAL, &strRet)) THROW_EXC(cERObjectNotFound);
		
	return strRet;	
}

void CConfigData::SetPingXML( const wxString& val)
{
	if ( !m_config ) THROW_EXC(cERWrongObjectState);
	m_config->Write(CFG_PING_VAL, val);
}

wxString CConfigData::GetFilters(cSearchOwnerID cID)
{
	wxString strRet;
	
	if ( !m_config ) THROW_EXC(cERWrongObjectState);

	wxString strKey(CFG_FILTERS_VAL);
	strKey+=wxT("_");
	strKey+=wxString::Format(wxT("%d"),(int)cID);

	if (!m_config->Read(strKey, &strRet)) THROW_EXC(cERObjectNotFound);
		
	return strRet;
}

void CConfigData::SetFilters( cSearchOwnerID cID, const wxString& val)
{
	if ( !m_config ) THROW_EXC(cERWrongObjectState);

	wxString strKey(CFG_FILTERS_VAL);
	strKey+=wxT("_");
	strKey+=wxString::Format(wxT("%d"),(int)cID);

	m_config->Write(strKey, val);
}

//MAC list
wxString CConfigData::GetMACList(cMACBlockRuleId cID)
{
	wxString strRet;
	
	if ( !m_config ) THROW_EXC(cERWrongObjectState);

	wxString strKey(CFG_MACLST_VAL);
	strKey+=wxT("_");
	strKey+=wxString::Format(wxT("%d"),(int)cID);

	if (!m_config->Read(strKey, &strRet)) THROW_EXC(cERObjectNotFound);
		
	return strRet;
}

void CConfigData::SetMACList(cMACBlockRuleId cID, const wxString& val)
{
	if ( !m_config ) THROW_EXC(cERWrongObjectState);

	wxString strKey(CFG_MACLST_VAL);
	strKey+=wxT("_");
	strKey+=wxString::Format(wxT("%d"),(int)cID);

	m_config->Write(strKey, val);
}

//advanced opt string
wxString CConfigData::GetAdvancedOptXML()
{
	wxString strRet;
	
	if ( !m_config ) THROW_EXC(cERWrongObjectState);
	if (!m_config->Read(CFG_OPT_XML_VAL, &strRet)) THROW_EXC(cERObjectNotFound);
		
	return strRet;
}

void CConfigData::SetAdvancedOptXML(const wxString& strVal)
{
	if ( !m_config ) THROW_EXC(cERWrongObjectState);
	m_config->Write(CFG_OPT_XML_VAL, strVal);
}

//Port Forwarding
wxString CConfigData::GetPortForwardXML()
{
	wxString strRet;
	
	if ( !m_config ) THROW_EXC(cERWrongObjectState);
	if (!m_config->Read(CFG_PORTFORWARD_XML_VAL, &strRet)) THROW_EXC(cERObjectNotFound);
		
	return strRet;
}

void CConfigData::SetPortForwardXML(const wxString& strVal)
{
	if ( !m_config ) THROW_EXC(cERWrongObjectState);
	m_config->Write(CFG_PORTFORWARD_XML_VAL, strVal);
}

//RouterID
wxString CConfigData::GetRouterID()
{
	wxString strRet;
	
	if ( !m_config ) THROW_EXC(cERWrongObjectState);
	if (!m_config->Read(CFG_ROUTER_ID_VAL, &strRet)) THROW_EXC(cERObjectNotFound);
		
	return strRet;
}

void CConfigData::SetRouterID(const wxString& strVal)
{
	try
	{
		if ( m_config )
		{
			m_config->Write(CFG_ROUTER_ID_VAL, strVal);
		}
	}catch(...) {}
}

//ADSL
wxString CConfigData::LoadADSLXML()
{
	wxString strRet;

	if ( m_config )
	{
		m_config->Read(CFG_ADSL_XML_VAL, &strRet);
	}

	return strRet;

}

void CConfigData::SaveADSLXML(const wxString& strVal)
{
	if ( m_config )
	{
		m_config->Write(CFG_ADSL_XML_VAL, strVal);
	}
}

//IPTABLES chain names seed
wxString CConfigData::GetUniqueSeed()
{
	wxString strRet;

	if ( m_config )
	{
		m_config->Read(CFG_CHAINSEED_XML_VAL, &strRet);
	}

	return strRet;
}

void CConfigData::SetUniqueSeed(const wxString& strVal)
{
	if ( m_config )
	{
		m_config->Write(CFG_CHAINSEED_XML_VAL, strVal);
	}
}

void CConfigData::SetIPResolve(bool bEnable)
{
	if ( !m_config ) THROW_EXC(cERWrongObjectState);
	m_config->Write(CFG_IP_RESOLVE_VAL, bEnable);
}

bool CConfigData::GetIPResolve()
{
	long lRet(0);

	if ( !m_config ) THROW_EXC(cERWrongObjectState);
	if ( !m_config->Read(CFG_IP_RESOLVE_VAL, &lRet) ) lRet = 0;

	return (bool)lRet;
}
